using Microsoft.Phone.Tasks;
using System.Windows.Input;
using System;
using TaxiPlease.PhoneHelpers;

namespace TaxiPlease
{
	public class SearchContactsCommand : ICommand
	{
		private readonly OptionsViewModel options;

		public SearchContactsCommand(OptionsViewModel options)
		{
			this.options = options;
		}

		public bool CanExecute(object parameter)
		{
			return this.options != null;
		}

		public event EventHandler CanExecuteChanged;

		public void Execute(object parameter)
		{
			PhoneNumberChooserTask task = new PhoneNumberChooserTask();
			task.Completed += new System.EventHandler<PhoneNumberResult>(task_Completed);
			task.Show();
		}

		private void task_Completed(object sender, PhoneNumberResult e)
		{
			(sender as PhoneNumberChooserTask).Completed -= task_Completed;

			if (e.TaskResult == TaskResult.OK)
			{
				this.options.TaxiPhoneNumber = e.PhoneNumber;
			}
		}
	}
}